import 'package:dio/dio.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_list_resp.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_list_resp_v2.dart';
import 'package:schedule_self_client/common/serializers.dart';
import 'package:schedule_self_client/common/simple_resp.dart';

class TaskApi {
  static final Dio _dio = Dio(BaseOptions(
    baseUrl: 'http://1.13.79.183:8080',
    headers: {},
  ));

  static Future<TaskListResp> getTasks(int pageIndex, int pageSize) async {
    Response response = await _dio.get('/task/v1/getTasks',
        queryParameters: {"pageIndex": pageIndex, "pageSize": pageSize});
    TaskListResp? taskListResp =
        serializers.deserializeWith(TaskListResp.serializer, response.data);
    if (taskListResp != null) {
      return taskListResp;
    }
    return Future.value(TaskListResp());
  }

  static Future<TaskListRespV2> getTasksV2() async {
    Response response = await _dio.post('/task/v2/getTasks', data: {});
    TaskListRespV2? taskListRespV2 =
        serializers.deserializeWith(TaskListRespV2.serializer, response.data);
    if (taskListRespV2 != null) {
      return taskListRespV2;
    }
    return Future.value(TaskListRespV2());
  }

  static Future<SimpleResp> taskCompleteFlagChange(int id) async {
    Response response = await _dio.post('/task/v1/taskCompleteFlagChange',
        options: Options(contentType: "application/json"),
        data: {
          "id": id,
        });
    SimpleResp? simpleResp =
        serializers.deserializeWith(SimpleResp.serializer, response.data);
    if (simpleResp != null) {
      return simpleResp;
    }
    return Future.value(SimpleResp());
  }
}
